package com.eastfair.home.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.home.dto.ReportDrawingCategoryAuditHistoryDTO;
import com.eastfair.home.dto.ReportDrawingCategoryAuditHistoryPageQuery;
import com.eastfair.home.dto.ReportDrawingCategoryAuditHistorySaveDTO;
import com.eastfair.home.dto.ReportDrawingCategoryAuditHistoryUpdateDTO;
import com.eastfair.home.entity.ReportDrawingCategoryAuditHistory;
import com.eastfair.home.service.ReportDrawingCategoryAuditHistoryService;
import com.eastfair.home.vo.ReportDrawingCategoryAuditHistoryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 报馆图纸分类审核记录历史
 * </p>
 *
 * @author dq
 * @date 2022-08-08
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/reportDrawingCategoryAuditHistory")
@Api(value = "ReportDrawingCategoryAuditHistory", tags = "报馆图纸分类审核记录历史")
@PreAuth(replace = "home:reportDrawingCategoryAuditHistory:")
public class ReportDrawingCategoryAuditHistoryController extends SuperController<ReportDrawingCategoryAuditHistoryService, Long, ReportDrawingCategoryAuditHistory, ReportDrawingCategoryAuditHistoryPageQuery, ReportDrawingCategoryAuditHistorySaveDTO, ReportDrawingCategoryAuditHistoryUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private ReportDrawingCategoryAuditHistoryService reportDrawingCategoryAuditHistoryService;

    @Override
    public void handlerResult(IPage<ReportDrawingCategoryAuditHistory> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<ReportDrawingCategoryAuditHistory> reportDrawingCategoryAuditHistoryList = list
                .stream()
                .map((map) -> ReportDrawingCategoryAuditHistory.builder().build())
                .collect(Collectors.toList());
        return R.success(baseService.saveBatch(reportDrawingCategoryAuditHistoryList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<ReportDrawingCategoryAuditHistoryPageQuery> params) {
        ReportDrawingCategoryAuditHistoryPageQuery pageQuery = params.getModel();
        //分页处理
        if (ContextUtil.getProjectId() != null) {
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 分页查询报馆图纸分类审核历史列表.
     *
     * @param pageParams the page params
     * @return the r
     */
    @ApiOperation("分页查询报馆图纸分类审核历史列表")
    @PostMapping("/query/reportDrawingCategoryAuditHistory")
    public R<Page<ReportDrawingCategoryAuditHistoryVO>> queryReportDrawingCategoryAuditHistory(@RequestBody
                                                                                               PageParams<ReportDrawingCategoryAuditHistoryDTO> pageParams) {
        log.info("queryReportDrawingCategoryAuditHistory - 分页查询报馆图纸分类审核历史列表, pageParams={}", pageParams);
        try {
            Page<ReportDrawingCategoryAuditHistoryVO> page = reportDrawingCategoryAuditHistoryService.listHistoryVOs(pageParams);
            return R.successDef(page);
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 场馆审核更新审核历史.
     *
     * @param historyDTO the history dto
     * @return the r
     */
    @PostMapping("/update/venueAuditStatus")
    public R<Boolean> updateVenueAuditStatus(@RequestBody ReportDrawingCategoryAuditHistoryDTO historyDTO) {
        log.info("updateVenueAuditStatus - 场馆审核更新审核历史, historyDTO={}", historyDTO);
        try {
            return R.successDef(reportDrawingCategoryAuditHistoryService.updateHistoryByVenue(historyDTO));
        } catch (BizException e) {
            return R.fail(e);
        }
    }
}
